Deploying a PHP and Laravel web application into AWS cloud using Elasticbeanstalk
Introduction
AWS Elastic Beanstalk is an cloud service for deploying certain web applications and services developed with for e.g. - Java, PHP, Node.js, Python, etc. on familiar servers such as Apache, Nginx, Passenger, and IIS.
You can just upload your code and Elastic Beanstalk automatically handles the deployment, from capacity maintenance, load balancing, auto-scaling to application monitoring. At the same time, one has full control over the AWS resources serving your application and can access the underlying resources at any time.
Overview of the task
Any website needs constant changes in it from time to time depending on the organisation requirements. When you make those changes in the local system you need to deploy those changes in the cloud as well for the customers to know about it.
In today’s world, cloud is the best way to serve your website globally for the customers around the world to access it. AWS is one of the most prominent of the cloud service and I used it to deploy the website into the cloud. More specifically, I made use of Elastic beanstalk to deploy the website into the AWS cloud, so I did not need to provision separate resources for my application, AWS does everything for me.
The website I deployed is in PHP language based on Laravel engine. This blog walks through the steps required to make the changes get deployed into the AWS Elastic beanstalk.
Components used
- Docker – I used docker command line to initialise a docker awscli container, where I run the various commands which would deploy the application to the beanstalk.
- PHP/Laravel – The website was written using PHP language. Laravel is a web application framework for PHP. A web framework provides a structure and starting point for creating your application, allowing you to focus on creating something where the design will be take care by the framework.
- AWS Service – The cloud service which will be hosting the application. I used Elastic beanstalk engine 7.3 Laravel based as the environment configuration.
Procedure
Step 1 – Installing docker on the system.
Since I am using docker cli, so I installed it using any of the two links given below
https://docs.docker.com/toolbox/toolbox_install_windows/
https://github.com/docker/toolbox/releases
Step 2 – Setting up the project on the system
I installed all the relevant PHP/Laravel related dependencies required in the website. One can use the steps required for this as per the framework and environment one is using.
Step 3 – Preparing the AWS setup and containers
- Create an IAM user for the account in which one is deploying the AWS resources ( snip below)
- Next, in the file C:/Users/(username)/.aws/credentials, you have make changes to insert your IAM User’s ACCESS KEY and SECRET ACCESS KEY before you can do anything with the AWS. (snip below)
The [cmin] written on the top is the profile name which will be used in the later stage.
- Install and intiate the AWS cli container on Docker and move onto the next step. You can also directly install the awscli form the web. I used a docker-compose file to run the same.
docker-compose --compatibility run --rm awscli bash
Step 4 – initialising the beanstalk application
- Go into the website directory.
- Run the following commands (change the name of the application and the platform based on your need).
mkdir -p .elasticbeanstalk/saved_configs eb init (name of application) / --platform "arn:aws:elasticbeanstalk:ap-south-1::platform/PHP 7.3 running on 64bit Amazon Linux 2/3.1.6" / --keyname (name of the key pair) / --region (AWS region) / --profile cmin
These commands when executed , creates an Elastic beanstalk application in the AWS. The output of the AWS console is like this:
Step 5 – creating beanstalk staging environment
- Now that we have created the aws beanstalk application, we need to create the aws beanstalk staging environment. Before proceeding for uploading the website into production environment, you need to make sure that first everything is working fine in the aws staging environment. So we are going for first deploying the website in the staging environment
Use the following commands:
config_eb_setting_from_env.rb cmin-website staging eb config put cmin-website-staging-sc --profile cmin eb create cmin-website-staging --cfg cmin-website-staging-sc --profile cmin
here, “cmin-website-staging” is the name of the environment
output of the AWS console :
- Copy the url above as show in the banstalk console and then paste it In C:/Users/(username)/git/(project folder)/server/env/.env.staging, in the place of APP_URL and ASSET_URL, and then save the file. (file structure would seem like this)
APP_NAME= APP_ENV=staging APP_KEY= APP_DEBUG=false APP_URL= ASSET_URL=
- Next you have to terminate the previously created environment and then recreate the environment, use the command below to terminate
eb terminate cmin-website-staging --profile cmin –force
- Now you have to recreate environment using the commands in point 1 of step 4.
- Now when the environment creation is complete, then access the elastic beanstalk link, and you will be able to access the website.
Step 6 - Uploading the website into production environment
Unless and until everything is fine in the staging environment you cannot proceed to deploy into the production environment. The production is where our website will be live which will be visible to the customers. So it is critical. The name of the environment is cmin-website-production
- For uploading into the production environment, you need to run the following command:
cd /var/www/html config_eb_setting_from_env.rb cmin-website production eb config put cmin-website-production-sc --profile cmin eb create cmin-website-production --cfg cmin-website-production-sc --profile cmin
- Now login into the EC2 instance by running
eb ssh cmin-website-production --profile cmin
- Once you have logged into the instance, now run the following commands
sudo su - cd /var/www/html export $(cat /opt/elasticbeanstalk/deployment/env | grep -v ^# | xargs) mysql -u $DB_USERNAME -p$DB_PASSWORD -h $DB_HOST -P $DB_PORT -e 'show databases;' mysql -u $DB_USERNAME -p$DB_PASSWORD -h $DB_HOST -P $DB_PORT $DB_DATABASE -e 'show tables;' php artisan migrate:fresh --seed --force
- When you now access the website using the beanstalk link in the production environment, the information which you have updated the website will be visible and all the customers can see it.
Conclusion
Elastic beanstalk is an excellent service to deploy to the cloud as there is no need of manually provisioning any resource and hence reduces the human intervention. I am looking forward to utilize my learning in forthcoming CMIN projects.